<template>
  <div class="upload">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :http-request="HttpRequest"
      :before-upload="beforeAvatarUpload"
    >
      <div v-if="type === 'img'">
        <img v-if="value" :src="value" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </div>
      <div v-else>
        <video controls v-if="value" :src="value" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </div>
    </el-upload>
    <div v-if="value" class="del" @click="del">
      <i class="el-icon-delete-solid" />
    </div>
  </div>
</template>

<script>
import { uploadFileAPI } from '@/api/questions'
export default {
  props: {
    type: {
      type: String,
      default: 'img'
    },
    value: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      imageUrl: '',
      videoUrl: '',
      show: false
    }
  },
  methods: {
    async HttpRequest (file) {
      const ud = new FormData()
      ud.append('file', file.file)
      const res = await uploadFileAPI(ud)
      if (res.code === '205') {
        this.$message.error(res.message)
      }
      const data = `${process.env.VUE_APP_URL}/${res.data.url}`
      if (this.type === 'img') {
        this.$emit('input', data)
      } else {
        this.$emit('input', data)
      }
    },
    // 上传前
    beforeAvatarUpload (file) {
      if (this.type === 'img') {
        const bol =
          file.type === 'image/png' ||
          file.type === 'image/jpeg' ||
          file.type === 'image/jpg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('上传的图片大小不能超过 2MB!')
        }
        if (!bol) {
          this.$message.error('上传的图片格式错误!')
        }
        return isLt2M && bol
      } else {
        const bol = file.type === 'video/mp4'
        const limit = file.size / 1024 / 1024 < 8
        if (!limit) {
          this.$message.error('上传的视频大小不能超过 2MB!')
        }
        if (!bol) {
          this.$message.error('上传的视频格式错误!')
        }
        return limit && bol
      }
    },
    del () {
      this.$emit('input', '')
    }
  }
}
</script>

<style lang="scss" scoped>
.upload {
  position: relative;
  ::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .btn {
    text-align: center;
    margin-top: 70px;
  }
  .imgBtn {
    display: flex;
    align-items: center;
    cursor: pointer;
  }
  &:hover {
    .del {
      display: block;
    }
  }
  .del {
    color: red;
    position: absolute;
    right: 5px;
    top: 5px;
    display: none;
    cursor: pointer;
  }
}
</style>
